package com.hxd.otc.module.convenience.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.ConvenienceBusinessTypeEnum;
import com.hxd.otc.module.convenience.bean.ChooseHydropowerCityBean;
import com.hxd.otc.module.convenience.bean.ConvenienveHistoryBean;
import com.hxd.otc.module.convenience.bean.NorGoodsBean;
import com.hxd.otc.module.convenience.bean.OrderInfoBean;
import com.hxd.otc.module.convenience.bean.RateBean;
import com.hxd.otc.module.convenience.bean.WaterCoalBillBean;
import com.hxd.otc.module.convenience.contract.HydropwerDetailContract;
import com.hxd.otc.module.convenience.fragment.PayDialogFragment;
import com.hxd.otc.module.convenience.presenter.HydropwerDetailPresenter;
import com.hxd.otc.module.login.activity.LoginActivity;
import com.hxd.otc.utils.GlideUtils;
import com.hxd.otc.utils.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class HydropwerDetailActivity extends BaseActivity<HydropwerDetailPresenter> implements HydropwerDetailContract.View {

    @BindView(R.id.iv_icon)
    ImageView ivIcon;

    @BindView(R.id.tv_label)
    TextView tvLabel;

    @BindView(R.id.tv_payment_unit)
    TextView tvPaymentUnit;

    @BindView(R.id.tv_number)
    TextView tvNumber;

    @BindView(R.id.bt_recharge)
    Button btRecharge;

    @BindView(R.id.tv_enabel_money)
    TextView tvEnabelMoney;

    @BindView(R.id.tv_urrent_arrears)
    TextView tvUrrentArrears;

    @BindView(R.id.tv_payment_method)
    TextView tvPaymentMethod;

    @BindView(R.id.tv_priceUnit)
    TextView tvPriceUnit;

    @BindView(R.id.et_money)
    EditText etMoney;

    NorGoodsBean norGoodsBean;
    WaterCoalBillBean billBean;

    //下面二者 只能出现一个
    ConvenienveHistoryBean historyBean;
    ChooseHydropowerCityBean hydropowerCityBean;
    public static final String NORGOODSBEAN = "goodBean";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hydropwer_detail;
    }

    @Override
    protected void initView() {
        if (getIntent().getExtras().containsKey("ConvenienveHistoryBean")) {//从水电费首页标签进来的
            historyBean = (ConvenienveHistoryBean) getIntent().getExtras().getSerializable("ConvenienveHistoryBean");
        }
        if (getIntent().getExtras().containsKey("ChooseHydropowerCityBean")) {// 从选择缴费机构进来的
            hydropowerCityBean = (ChooseHydropowerCityBean) getIntent().getExtras().getSerializable("ChooseHydropowerCityBean");
        }
        norGoodsBean = (NorGoodsBean) getIntent().getExtras().getSerializable(NORGOODSBEAN);

        billBean = (WaterCoalBillBean) getIntent().getExtras().getSerializable("billBean");

        tvLabel.setText(norGoodsBean.getGoodsName());
        GlideUtils.loadNormal(UIUtils.getContext(), UIUtils.getServiceImageUrlByPath(norGoodsBean.getGoodsIcon()), ivIcon);

        tvPaymentUnit.setText(billBean.customerAddress);
        tvNumber.setText(billBean.accountNo);
        tvPaymentMethod.setText(norGoodsBean.getSymbol());
        tvPriceUnit.setText(norGoodsBean.getPriceUnit());
        tvEnabelMoney.setText(billBean.balance + "元");
        tvUrrentArrears.setText(billBean.payAmount + "元");

    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected HydropwerDetailPresenter createPresenter() {
        return new HydropwerDetailPresenter(this);
    }

    PayDialogFragment payDialogFragment;
//    PayMethodDialog payMethodDialog;

    @OnClick({R.id.iv_clear, R.id.bt_recharge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_clear:
                etMoney.setText("");
                break;

            case R.id.bt_recharge:
                if (TextUtils.isEmpty(etMoney.getText().toString())) {
                    showToast(getString(R.string.enter_money_hint));
                    return;
                }

                btRecharge.setEnabled(false);
                mPresenter.getRate(etMoney.getText().toString() + "", "CNY", tvPaymentMethod.getText().toString());

                break;
        }
    }

    @Override
    public void setRate(RateBean bean, String sellPrice) {
        if (!UserHelper.isLogin()) {
            Bundle bundle = new Bundle();
            bundle.putBoolean(LoginActivity.EXTRA_FINISH_SKIP_MAIN, false);
            $startActivity(LoginActivity.class, bundle);
            return;
        }
        btRecharge.setEnabled(true);
        OrderInfoBean orderInfoBean = new OrderInfoBean();
        orderInfoBean.setModuleId(norGoodsBean.getId() + "");
        orderInfoBean.setRealSymbol(bean.symbol);
        orderInfoBean.setVolume(Double.valueOf(bean.volume));
        orderInfoBean.setRechargeAccount(billBean.accountNo);
        orderInfoBean.setItemNum(etMoney.getText().toString());

        if (historyBean != null) {
            orderInfoBean.setRemark(historyBean.remark);
            orderInfoBean.setItemId(historyBean.itemId);
            orderInfoBean.setAddress(historyBean.address);
        }
        if (hydropowerCityBean != null) {
            orderInfoBean.setAddress(hydropowerCityBean.rechargeUnit);
            orderInfoBean.setItemId(hydropowerCityBean.itemId);
            orderInfoBean.setRemark(hydropowerCityBean.remarks);
        }

        orderInfoBean.setBillCycle(billBean.billCycle);
        orderInfoBean.setContractNo(billBean.contractNo);
        orderInfoBean.setPriceUnit(norGoodsBean.getPriceUnit());
        orderInfoBean.setGoodsName(norGoodsBean.getGoodsName() + getString(R.string.top_up_nationwide));
        orderInfoBean.setBusinessType(ConvenienceBusinessTypeEnum.LIVING);
        orderInfoBean.setModelTitle(norGoodsBean.getGoodsName());
        orderInfoBean.setSellPrice(sellPrice);

        if (payDialogFragment == null) {
            payDialogFragment = PayDialogFragment.getInstance().setData(orderInfoBean);
        } else {
            UIUtils.dismissDialogFragment(payDialogFragment);
        }
        payDialogFragment.setData(orderInfoBean);
        payDialogFragment.show(getSupportFragmentManager(), PayDialogFragment.class.getSimpleName());
    }


    @Override
    public void onError(int code) {
        super.onError(code);
        btRecharge.setEnabled(true);
    }

}
